<?php
    include_once("../action/checkAuthenticationAction.php");
    include_once("../service/categoryService.php");
    include_once("../util/constant.php");
    
    include_once("../util/authUtil.php");
    $perm_array = $_SESSION["permissions"];
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<title>Dashboard</title>
	<?php include("import-css.php");?>	
</head>
<body>
	<?php include("header.php");?>
    
    <div class="container-fluid">
		<div class="row-fluid">
            <!--Menu-->
            <?php include("menu.php");?>
            <!--Content-->
            <div class="box span10 offset2">
    					<div class="box-header well" data-original-title>
                            <?php
                                
                                if(!authUtil(array("edit category","create category","view category"),$perm_array,false)){
                                    echo "<script>window.location.href='../admin/index.php';</script>";
                                }
                                
                                $require_perm = array("create category");
                                if(authUtil($require_perm,$perm_array,true)){
                            ?>
    						<a class="btn btn-primary" href="./category-detail.php?action=<?php echo NEW_ACTION; ?>">
                                <i class="icon-edit icon-white"></i>Thêm mới
                            </a>
                            <?php
                                }
                            ?>
    						<div class="box-icon">
    							<a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
    						</div>
    					</div>
    					<div class="box-content">
    						<table class="table table-striped table-bordered bootstrap-datatable datatable">
    						  <thead>
    							  <tr>
    								  <th>Nhóm sản phẩm</th>
    								  <th>Thuộc nhóm</th>
    								  <th>Chức năng</th>
    							  </tr>
    						  </thead>   
    						  <tbody>
								<?php
									function getHierachyAttributesOfCategory($cat_id, &$attrs){
										$temps = getAttributesOfCategory($cat_id);
										while($aRecord = mysql_fetch_array($temps)){
											array_push($attrs,$aRecord);
										}
                                        $tmp = getCategory($cat_id);
										$parentId = $tmp["CAT_PARENT_ID"];
										if($parentId != 0){
											getHierachyAttributesOfCategory($parentId,$attrs);
										}
										
									}
								?>
                                <?php
								$attrs = array();
                                $categories = getAllCategory();
                                    while($category = mysql_fetch_array($categories)){
                                ?>
                                        <tr>
                                            <td><?php echo $category['CAT_NAME'] ?></td>
                                            <td class="center"><?php echo $category['PARENT_CAT_NAME'] ?></td>
                                            <td class="center">
                                                <a href="#categoryModal<?php echo $category['CAT_ID']; ?>" role="button" class="btn btn-success" data-toggle="modal">
                                                    <i class="icon-zoom-in icon-white"></i>  
                                                    Xem thông tin
                                                </a>
                                                <!--Preview Category Information Modal-->
                                                <div class="modal hide fade" id="categoryModal<?php echo $category['CAT_ID']; ?>">
                                                    <div class="modal-header">
                                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                                        <?php echo $category['CAT_NAME']; ?>
                                                    </div>
                                                    <div class="modal-body">
                                                        <strong>Thuộc tính:</strong><br/>
                                                    <?php
                                                        $attrs = array();
														getHierachyAttributesOfCategory($category['CAT_ID'],$attrs);
                                                        foreach($attrs as $attr){
                                                            echo $attr["ATT_NAME"]."<br/>";
                                                        }
                                                    ?>
                                                    </div>
                                                    <div class="modal-footer">
                                                        
                                                    </div>
                                                </div>
                                                <?php
                                                    $require_perm = array("edit category","create category");
                                                    if(authUtil($require_perm,$perm_array,false)){
                                                ?>
                                                        <a class="btn btn-info" href="./category-detail.php?action=<?php echo EDIT_ACTION; ?>&id=<?php echo $category['CAT_ID']; ?>">
                                                            <i class="icon-edit icon-white"></i>  
                                                            Cập nhật
                                                        </a>
                                                <?php
                                                    }
												?>
												<?php
                                                    $require_perm = array("delete category");
                                                    if(authUtil($require_perm,$perm_array,true)){
													   if(isCateDeletable($category["CAT_ID"])){
												?>
                                                        <a class="btn btn-danger" href="../action/deleteCategory.php?id=<?php echo $category["CAT_ID"]; ?>">
                                                            <i class="icon-trash icon-white"></i> 
                                                            Xóa
                                                        </a>
												<?php
													   }
                                                    }
												?>
                                            </td>
                                        </tr>
                                <?php
                                    }
                                ?>
    						  </tbody>
    					  </table>            
                        </div>
            </div><!--/span-->
        </div>
    </div>

    <?php include("footer.php");?>
		
	</div><!--/.fluid-container-->

	<?php include("import-js.php");?>	
    
</body>
</html>
